package cn.kgc.service;

import cn.kgc.mapper.DmOrderMapper;
import cn.kgc.pojo.DmOrder;
import cn.kgc.utils.Constants;
import cn.kgc.utils.EmptyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 北大课工场
 */
@RestController
public class RestDmOrderService {

    @Autowired
    private DmOrderMapper dmOrderMapper;

    @RequestMapping(value = "/getDmOrderById", method = RequestMethod.POST)
    public DmOrder getDmOrderById(@RequestParam("id") Long id) throws Exception {
        return dmOrderMapper.getDmOrderById(id);
    }

    @RequestMapping(value = "/getDmOrderListByMap", method = RequestMethod.POST)
    public List<DmOrder> getDmOrderListByMap(@RequestParam Map
            <String, Object> param) throws Exception {
        return dmOrderMapper.getDmOrderListByMap(param);
    }

    @RequestMapping(value = "/getDmOrderCountByMap", method = RequestMethod.POST)
    public Integer getDmOrderCountByMap(@RequestParam Map
            <String, Object> param) throws Exception {
        return dmOrderMapper.getDmOrderCountByMap(param);
    }

    @RequestMapping(value = "/qdtxAddDmOrder", method = RequestMethod.POST)
    public Integer qdtxAddDmOrder(@RequestBody DmOrder dmOrder) throws Exception {
        dmOrder.setCreatedTime(new Date());
        System.out.println(dmOrder.getId());
        dmOrderMapper.insertDmOrder(dmOrder);
        System.out.println(dmOrder.getId());

        //Order表里面，返回一个OrderId
        int i = dmOrder.getId().intValue();
        return i;
    }

    @RequestMapping(value = "/qdtxModifyDmOrder", method = RequestMethod.POST)
    public Integer qdtxModifyDmOrder(@RequestBody DmOrder dmOrder) throws Exception {
        dmOrder.setUpdatedTime(new Date());
        return dmOrderMapper.updateDmOrder(dmOrder);
    }

    @RequestMapping(value = "/qddelDmOrder", method = RequestMethod.GET)
    public Integer qdtxDelDmOrder(@RequestParam("id") Long id) throws Exception {

        return dmOrderMapper.deleteDmOrderById(id);
    }


    /**
     * 校验这个订单是否支付过
     *
     * @param out_trade_no 订单号
     * @param type         支付类型，支付宝，微信
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "processed", method = RequestMethod.POST)
    public boolean processedPay(@RequestParam("orderNo") String out_trade_no, @RequestParam("type") Integer type) throws Exception {
        //1.对参数校验，out_trade_no  type 非空校验 throw new BaseException
        //2.订单存在判断 out_trade_no throw new BaseException
        //3.根据out_trade_no 后去到DmOrder订单
        Map map = new HashMap();
        map.put("orderNo", out_trade_no);
        DmOrder dmOrder = (DmOrder) dmOrderMapper.getDmOrderListByMap(map).get(0);
        String tradeNo = Constants.PayMethod.WEIXIN == type ? dmOrder.getWxTradeNo() : dmOrder.getAliTradeNo();
        //如果交易号为空，并且订单状态为未支付的，就认为这个订单没有被支付过
        return EmptyUtils.isEmpty(tradeNo) && dmOrder.getOrderType().equals(0);

    }

    @RequestMapping(value = "flushCancelOrder", method = RequestMethod.POST)
    public Integer flushCancelOrder() {
        Integer integer = dmOrderMapper.flushCancelOrder();
        return integer;
    }
}
